package com.project.finance.mobile.debit.service.impl;

import com.project.finance.common.cosconfig.UploadImgConfig;
import com.project.finance.common.mapper.TbDebitOrderMapper;
import com.project.finance.common.mapper.TbPaybackOrderMapper;
import com.project.finance.common.po.TbDebitOrder;
import com.project.finance.common.po.TbPaybackOrder;
import com.project.finance.mobile.debit.service.DebitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: 贺谋昌
 * @Date: 2019/12/10 15:04
 * @Description:
 */
@Service(value = "DebitServiceImplM")
public class DebitServiceImpl implements DebitService {
    @Autowired
    TbDebitOrderMapper debitOrderMapper;

    @Autowired
    UploadImgConfig uploadImgConfig;

    @Autowired
    TbPaybackOrderMapper paybackOrderMapper;

    @Override
    public boolean uploadIdFont(MultipartFile martipart, HttpServletRequest request, String imgType) {
        try {
            uploadImgConfig.uploadFile(martipart, request.getParameter("userId") + imgType);
        } catch (IOException e) {
//            e.printStackTrace();
            return false;
        }

        return true;
    }

    @Override
    public void insertDebit(TbDebitOrder order) {
        debitOrderMapper.insert(order);
    }

    @Override
    public void insertPayback(TbPaybackOrder paybackOrder) {
        paybackOrderMapper.insert(paybackOrder);
    }

    /**
     * 根据Id查询出借贷数据集合
     *
     * @param idList
     * @return
     */
    @Override
    public List<TbDebitOrder> getTbDebitListByIds(List<String> idList) {
        List<Integer> ids = new ArrayList<>();
        for (String id : idList) {
            ids.add(Integer.valueOf(id));
        }
        Example example = new Example(TbDebitOrder.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("debitOrderId", ids);
        List<TbDebitOrder> tbDebitOrders = debitOrderMapper.selectByExample(example);
        debitOrderMapper.deleteByIdList(ids);
        return tbDebitOrders;
    }

    @Override
    public void updateDebitList(List<TbDebitOrder> debitList) {
        debitOrderMapper.insertList(debitList);
    }
}
